import VueRouter from "vue-router";
import store from '@/store';
import Vue from "vue";
Vue.use(VueRouter);
import Home from "@/views/home";
import Test from "@/views/test";
import Task from "@/views/task";
import User from "@/views/user";
import Role from "@/views/role";
import Login from "@/views/login";
import Menu from "@/views/menu";


const router = new VueRouter({
    routes: [
        {
            name: "login",
            path: "/login",
            component: Login,
        },
        {
            path: "/home",
            component: Home,
        },
        {
            path: "/test",
            component: Test,
        },
        {
            path: "/task",
            component: Task,
        },
        {
            path: "/user",
            component: User,
        },
        {
            path: "/role",
            component: Role,
        },
        {
            path: "/menu",
            component: Menu,
        },
        {
            path: '/',
            redirect: '/login'
        }
    ],
});


router.beforeEach((to, from, next) => {
    var islogin = store.state.user.token;
    var isTokenExpiresTime = new Date(store.state.user.tokenExpiresTime) > new Date();
    if (to.name !== 'login' && (!islogin || !isTokenExpiresTime)) next({ name: 'login' })
    else next()
})
export default router;